const TerserPlugin = require('terser-webpack-plugin');
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const PurgeCSSPlugin = require('purgecss-webpack-plugin');
const glob = require('glob');
const path = require('path');

const PATHS = {
  src: path.join(__dirname, 'src')
}

module.exports = {
  mode: 'production',
  devtool: false,
  module: {
    rules: [
      {
        test: /\.less$/,
        use: [MiniCssExtractPlugin.loader, 'css-loader', 'postcss-loader', 'less-loader'],
      },
      {
        test: /\.(sass|scss)$/,
        use: [MiniCssExtractPlugin.loader, 'css-loader', 'postcss-loader', 'sass-loader'],
      }
    ]
  },
  plugins: [
    new MiniCssExtractPlugin({
      filename: 'assets/styles/[name][contenthash:8].css',
      publicPath: '/assets/styles' // css访问前缀
      // chunkFilename: '[id].css'
    }),
    new BundleAnalyzerPlugin({
      analyzerMode: 'disabled',
      generateStatsFile: true
    }),
    new PurgeCSSPlugin({
      paths: glob.sync(`${PATHS.src}/**/*.scss`, { nodir: true })
    })
  ],
  optimization: {
    // minimize: true, // 开启压缩, 这样minimizer里的配置才会生效, mode: production下自动开启
    minimizer: [
      new TerserPlugin({  // 开启并配置压缩js,production下自动开启,除非你想改参数
        terserOptions: {
          output: {
            comments: false,
          }
        }
      }),
      new CssMinimizerPlugin({  // 开启并配置压缩css
      
      })
    ]
  }
}
